package com.weilx959.spt.plugin.e2sp.utils.expression.deparser;

import com.weilx959.spt.core.utils.Assert;
import com.weilx959.spt.plugin.e2sp.constant.ExcelToSqlConstants;
import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.TimeValue;
import net.sf.jsqlparser.expression.TimestampValue;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Optional;

/**
 * oracle表达式解解析器
 *
 * @author weilx
 * @since 2024-05-15
 */
public class OracleExpressionDeParser extends ParentExpressionDeParser {
    @Override
    public void visit(DateValue dateValue) {
        Date date = Optional.ofNullable(dateValue).map(DateValue::getValue).orElse(null);
        Assert.notNull(date, "dateValue is null");
        String format = dateFormatter.format(date);
        buffer.append("TO_DATE('").append(format).append("', 'yyyy-mm-dd')");
    }

    @Override
    public void visit(TimestampValue timestampValue) {
        Timestamp timestamp = Optional.ofNullable(timestampValue).map(TimestampValue::getValue).orElse(null);
        Assert.notNull(timestamp, "timestampValue is null");
        String format = timestampFormatter.format(timestamp);
        buffer.append("TO_DATE('").append(format).append("', 'yyyy-mm-dd hh24:mi:ss')");
    }

    @Override
    public void visit(TimeValue timeValue) {
        Time time = Optional.ofNullable(timeValue).map(TimeValue::getValue).orElse(null);
        Assert.notNull(time, "timeValue is null");
        String format = timeFormatter.format(time);
        buffer.append("TO_DATE('").append(format).append("', 'hh24:mi:ss')");
    }

    @Override
    public void visit(StringValue stringValue) {
        if (ExcelToSqlConstants.SYSDATE_FUNCTION.equals(stringValue.getValue())) { // 系统日期
            buffer.append(ExcelToSqlConstants.SYSDATE_FUNCTION);
        } else {
            super.visit(stringValue);
        }
    }
}
